#include "TcpClient.hpp"
//./client + ip + port
#include "Sock.hpp"
#include "Protocol.hpp"
//./client ip + port
using namespace Protocol;
Request Parse(std::string s)
{
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        exit(USAGE_ERR);
    }
    std::string ip = argv[1];
    uint16_t port = atoi(argv[2]);
    Log logs;
    
    Sock socks;
    socks.Socket();
    socks.Connect(ip, port);
    logs(ErrorLevel::Debug, "init success: sockt:%d", socks.socket_);

    std::string inbuffer;
    while (true)
    {
        std::cout << "data1# ";
        Request rq;

        std::cin >> rq.x_;

        std::cout << "data2# ";
        std::cin >> rq.y_;

        std::cout << "data3# ";
        std::cin >> rq.op_;
        // 序列化
        std::string ret;
        rq.serialize(&ret);

        // 添加报头
        ret = AddHead(ret);

        // 开始发送
        send(socks.socket_, ret.c_str(), ret.size(), 0);

        // 开始读取
        std::string package;
START:
        int n = Readpackage(socks.socket_, &inbuffer, &package);
        if (n < 0)
        {
            close(socks.socket_);
            exit(READ_ERR);
        }
        else if (n == 0)
            goto START;
        // 到了这里就已经读取到了完整的数据报,先去除报头
        package = RemoveHead(package, n);

        //将字符串反序列化
        Response rp;
        rp.Deserialize(package); 
           
        cout<<"result: "<<rp.result_<<endl;
        cout<<"exitcode: "<<rp.exitcode_<<endl;
    }
    return 0;
}